define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'item/index' + location.search,
                    add_url: 'item/add',
                    edit_url: 'item/edit',
                    del_url: 'item/del',
                    multi_url: 'item/multi',
                    import_url: 'item/import',
                    table: 'item',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form",table.$commonsearch);
                $("input[name='classification_id']", form).addClass("selectpage")
                    .data("source", "classification/index")
                    .data("primaryKey", "id")
                    .data("field", "name");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                // commonSearch: false,
                exportDataType: 'basic',
                columns: [
                    [
                        {checkbox: true},
                        // {
                        //     field: 'classification.name',
                        //     title: '所属分类',
                        //     // addClass: selectpicker,
                        //     operate: 'LIKE',
                        //     searchList: $.getJSON('classification/index')
                        // },
                        {
                            field: 'classification_id',
                            title: '所属分类',
                            visible: false,
                            // searchList: $.getJSON("classification/index"),
                            data: 'data-live-search="true"'
                        },
                        {
                            field: 'classification.name',
                            title: '所属分类',
                            operate: false
                        },


                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'spec',
                            title: __('Spec'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {
                            field: 'supplier',
                            title: __('Supplier'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'contacts',
                            title: '联系人',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },

                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {
                            field: 'remark',
                            title: __('Remark'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'update_time',
                            title: __('Update_time'),
                            operate: false,
                            formatter: Table.api.formatter.datetime
                        },
                        // {field: 'delete_time', title: __('Delete_time'), operate: false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });
            $('[name="classification_id"]').addClass('selectpicker');

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
